package org.jeecg.modules.power.province.controller;

import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.power.province.entity.YlProvince;
import org.jeecg.modules.power.province.service.IYlProvinceService;
import org.jeecg.modules.power.province.vo.ShopProvinceEntity;
import org.jeecg.modules.power.shop.service.IYlShopService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: yl_province
 * @Author: jeecg-boot
 * @Date:   2024-07-19
 * @Version: V1.0
 */
@Api(tags="yl_province")
@RestController
@RequestMapping("/power/ylProvince")
@Slf4j
public class YlProvinceController extends JeecgController<YlProvince, IYlProvinceService> {
	@Autowired
	private IYlProvinceService ylProvinceService;

	 @Autowired
	 private IYlShopService ylShopService;//店铺
	
	/**
	 * 分页列表查询
	 *
	 * @param ylProvince
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "yl_province-分页列表查询")
	@ApiOperation(value="yl_province-分页列表查询", notes="yl_province-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<YlProvince>> queryPageList(YlProvince ylProvince,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<YlProvince> queryWrapper = QueryGenerator.initQueryWrapper(ylProvince, req.getParameterMap());
		Page<YlProvince> page = new Page<YlProvince>(pageNo, pageSize);
		IPage<YlProvince> pageList = ylProvinceService.page(page, queryWrapper);
		return Result.OK(pageList);
	}




	//业务地图
	@GetMapping(value = "/getShopProvinceList")
	public Result<?> getShopProvinceList() {
		List<Map> shopProvinceEntityList=new LinkedList<>();

		List<YlProvince>provinceList= ylProvinceService.list(new QueryWrapper<YlProvince>().select("province_name,short_name,lng,lat"));
		for (YlProvince province:provinceList){
			Map shopProvinceEntity=new HashMap();
			shopProvinceEntity.put("name",province.getShortName());
			List<String> value=new LinkedList<>();
			value.add(province.getLng());
			value.add(province.getLat());
			shopProvinceEntity.put("value",value);
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("color","#00EEFF");//色值
			shopProvinceEntity.put("itemStyle",jsonObject);
			//取出 系统地址
			int shopQuantity =ylShopService.getShopQuantity(province.getShortName());
			if (shopQuantity>0){
				shopProvinceEntity.put("shopQuantity",shopQuantity);
				shopProvinceEntityList.add(shopProvinceEntity);
			}


		}

		return Result.OK(shopProvinceEntityList);
	}


























	 /**
	 *   添加
	 *
	 * @param ylProvince
	 * @return
	 */
	@AutoLog(value = "yl_province-添加")
	@ApiOperation(value="yl_province-添加", notes="yl_province-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody YlProvince ylProvince) {
		ylProvinceService.save(ylProvince);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param ylProvince
	 * @return
	 */
	@AutoLog(value = "yl_province-编辑")
	@ApiOperation(value="yl_province-编辑", notes="yl_province-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody YlProvince ylProvince) {
		ylProvinceService.updateById(ylProvince);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "yl_province-通过id删除")
	@ApiOperation(value="yl_province-通过id删除", notes="yl_province-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		ylProvinceService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "yl_province-批量删除")
	@ApiOperation(value="yl_province-批量删除", notes="yl_province-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.ylProvinceService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "yl_province-通过id查询")
	@ApiOperation(value="yl_province-通过id查询", notes="yl_province-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<YlProvince> queryById(@RequestParam(name="id",required=true) String id) {
		YlProvince ylProvince = ylProvinceService.getById(id);
		if(ylProvince==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(ylProvince);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param ylProvince
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, YlProvince ylProvince) {
        return super.exportXls(request, ylProvince, YlProvince.class, "yl_province");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, YlProvince.class);
    }

}
